
#include "lib_jpeg.h"


#define  LCD_WIDTH      (1920)
#define  LCD_HEIGHT     (1080)


int show_jpeg5(const char *data_buff,int w ,int h)
{

	int i,j;
	int a = 0;
	int r = 0;
	int g = 0;
	int b = 0;

	int temp = 0;

	int width  = w;
	int height = h;

	int x_start = 0;
	int y_start = 0;

	long start_time = 0;
	long end_time   = 0;
	
	uint32_t word;
	//*****************************************************

	if(!data_buff)
		return 0;
	//*****************************************************

	x_start = (LCD_WIDTH  - width)/2;
	y_start = (LCD_HEIGHT - height)/2;

#if 0
	log_i("x_start=%d\n",x_start);
	log_i("y_start=%d\n",y_start);

	log_i("width =%d\n",width);
	log_i("height=%d\n",height);
#endif
	//*****************************************************

	start_time = platform_get_microsecond();

	for(i=0;i<LCD_HEIGHT;i++)
	{
		for(j=0;j<LCD_WIDTH;j++)
		{
			if((i >= y_start)&&(i < (y_start + height))&&(j >= x_start)&&(j < (x_start + width)))
			{
				b= (*(data_buff + 0))&0x000000ff;
				g= (*(data_buff + 1))&0x000000ff;
				r= (*(data_buff + 2))&0x000000ff;	
				
				data_buff = data_buff + 3;
			}
			else
			{
				r = 0;
				g = 0;
				b = 0;
			}

			word = ((r<<0)|(g<<8) |(b<<16));
			
			//buf.vaddr[(i)*buf.width+(j)] = word;

			rkdrm_show_argb(j,i,1,1,a,b,g,r);

		}
	}

	end_time = platform_get_microsecond();

	log_i("time =%d\n",end_time - start_time);
	//*****************************************************
}


int show_jpeg_0(const char *data_buff,int w ,int h)
{

	int width   = w;
	int height  = h;

	int x_start = 0;
	int y_start = 0;

	long start_time = 0;
	long end_time	= 0;
	
	//*****************************************************

	if(!data_buff)
		return 0;
	//*****************************************************

	x_start = (LCD_WIDTH  - width)/2;
	y_start = (LCD_HEIGHT - height)/2;
	//*****************************************************

#if 0
	log_i("x_start=%d\n",x_start);
	log_i("y_start=%d\n",y_start);

	log_i("width =%d\n",width);
	log_i("height=%d\n",height);
#endif
	//*****************************************************

	start_time = platform_get_microsecond();

	//*****************************************************

	rkdrm_show_buff_0(x_start,y_start,width,height,data_buff);
	//*****************************************************

	end_time = platform_get_microsecond();
	//*****************************************************

	//log_i("time =%d\n",end_time - start_time);
	//*****************************************************
}

int show_jpeg_1(const char *data_buff,int w ,int h)
{

	int width   = w;
	int height  = h;

	int x_start = 0;
	int y_start = 0;

	long start_time = 0;
	long end_time	= 0;
	
	//*****************************************************

	if(!data_buff)
		return 0;
	//*****************************************************

	x_start = (LCD_WIDTH  - width)/2;
	y_start = (LCD_HEIGHT - height)/2;
	//*****************************************************

#if 0
	log_i("x_start=%d\n",x_start);
	log_i("y_start=%d\n",y_start);

	log_i("width =%d\n",width);
	log_i("height=%d\n",height);
#endif
	//*****************************************************

	start_time = platform_get_microsecond();

	//*****************************************************

	rkdrm_show_buff_1(x_start,y_start,width,height,data_buff);
	//*****************************************************

	end_time = platform_get_microsecond();
	//*****************************************************

	//log_i("time =%d\n",end_time - start_time);
	//*****************************************************
}

int decode_and_show_jpeg_0(char *filename)
{

	int ret;

	int i = 0;
	int j = 0;
	
	int w = 0;
	int h = 0;

	int v_w = 0;
	int v_h = 0;

	int pos = 0;

	int swap_flag_w = 0;
	int swap_flag_h = 0;
	
	char* out_buff  = NULL;
	char* swap_buff = NULL;

	struct jpeg_file jf;

	//log_i("filename = %s\n",filename);

	jf.fp= fopen(filename, "rb");
	if (jf.fp== NULL) {
		log_e("can not open file,%s\n",filename);
		return -1;
	}
	
	memset(jf.filename,0,sizeof(jf.filename));
	memcpy(jf.filename,filename,strlen(filename));


	ret =is_jpeg_file(filename,&jf);
	if(ret < 0 ){
		log_e("not jpeg file");
		
    	fclose(jf.fp);
		return -1;
	}

	ret = decode_jpeg(filename,&jf);
	if(ret < 0 ){
		log_e("decode jpeg wrong\n");
		fclose(jf.fp);
	}
	//***********************************************************************************************
#if 0
	log_i("jf.filename=%s\n",jf.filename);
	log_i("jf.width=%d\n",jf.width);
	log_i("jf.height=%d\n",jf.height);
	log_i("jf.Bpp=%d\n",jf.Bpp);
	log_i("jf.size=%d\n",jf.width*jf.height*jf.Bpp);
	log_i("jf.size=%d\n",jf.size);
#endif
	//***********************************************************************************************


	if(jf.width%4){
		v_w = jf.width + 4 - jf.width%4;
		swap_flag_w = 1;
	}
	else{
		v_w = jf.width;
	}
	
	if(jf.height%4){
		v_h = jf.height + 4 - jf.height%4;
		swap_flag_h = 1;
	}
	else{
		v_h = jf.height;
	}

	//***********************************************************************************************
#if 0
	log_i("swap_flag_h=%d\n",swap_flag_h);
	log_i("swap_flag_w=%d\n",swap_flag_w);	
	log_i("v_w=%d\n",v_w);
	log_i("v_h=%d\n",v_h);	
#endif
	//***********************************************************************************************

	if(swap_flag_w||swap_flag_h)
	{
		swap_buff = (char*)malloc(v_w*v_h*jf.Bpp);
		memset(swap_buff,0x00,v_w*v_h*jf.Bpp);

		for(i = 0;i < jf.height;i++)
		{
			memcpy(swap_buff + i*v_w*jf.Bpp,jf.buffer + i*jf.width*jf.Bpp ,jf.width*jf.Bpp);	
		}
	}
	else
	{
		swap_buff = jf.buffer;
	}
	
	//***********************************************************************************************
#if 0

	w = (LCD_WIDTH*jf.width/LCD_HEIGHT);

	h = (LCD_WIDTH*jf.height/LCD_HEIGHT);

	log_i("w  =%d\n",w );
	log_i("h  =%d\n",h );

	rga_control2(MODE_RESIZE,jf.buffer,jf.width,jf.height,jf.Bpp,
							out_buff,w,h,jf.Bpp);

#elif 1

	//log_i("src_w = %d\n",jf.width);
	//log_i("src_h = %d\n",jf.height);


	if((jf.width >= LCD_WIDTH)&&(jf.height >= LCD_HEIGHT))
	{
		//log_i("---1---\n");

		w = LCD_WIDTH;
		h = LCD_HEIGHT;

		out_buff = (char*)malloc(w*h*jf.Bpp);
		memset(out_buff,0x00,w*h*jf.Bpp);

		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,out_buff,w,h,jf.Bpp);
	}
	else if((jf.width >= LCD_WIDTH)&&(jf.height < LCD_HEIGHT))
	{
		//log_i("---2---\n");
	
		w = (jf.width*LCD_HEIGHT/jf.height);
		if(w%4){
			w = w + 4 - w%4;
		}
		h = LCD_HEIGHT;

		out_buff = (char*)malloc(w*h*jf.Bpp);
		memset(out_buff,0x00,w*h*jf.Bpp);
		
		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,out_buff,w,h,jf.Bpp);
	}
	else if((jf.width < LCD_WIDTH)&&(jf.height >= LCD_HEIGHT))
	{
		//log_i("---3---\n");

		w = (jf.width*LCD_HEIGHT)/jf.height;
		if(w%4){
			w = w + 4 - w%4;
		}
		h = LCD_HEIGHT;
		
		out_buff = (char*)malloc(w*h*jf.Bpp);
		memset(out_buff,0x00,w*h*jf.Bpp);

		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,out_buff,w,h,jf.Bpp);
	}
	else 
	{
		//log_i("---4---\n");
		
		w = jf.width;
		if(w%4){
			w = w + 4 - w%4;
		}
		h = jf.height;
		if(h%4){
			h = h + 4 - h%4;
		}	
		
		out_buff = (char*)malloc(w*h*jf.Bpp);
		memset(out_buff,0x00,w*h*jf.Bpp);

		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,out_buff,w,h,jf.Bpp);
	}
#endif
	//***********************************************************************************************

	show_jpeg_0(out_buff,w,h);

	//***********************************************************************************************					

	if(out_buff){
		free(out_buff);
		out_buff = NULL; 
	}
	
	if(swap_flag_w||swap_flag_h){
		if(swap_buff){
			free(swap_buff);
			swap_buff = NULL; 
		}
	}
	//***********************************************************************************************
	

	fclose(jf.fp);


	free_jpeg(&jf);
	
	//***********************************************************************************************
	
	return 0;

}



int decode_and_show_jpeg_1(char *filename)
{

	int ret;

	int i = 0;
	int j = 0;
	
	int w = 0;
	int h = 0;

	int v_w = 0;
	int v_h = 0;

	int pos = 0;

	int swap_flag_w = 0;
	int swap_flag_h = 0;
	
	char* out_buff  = NULL;
	char* swap_buff = NULL;

	struct jpeg_file jf;

	//log_i("filename = %s\n",filename);

	jf.fp= fopen(filename, "rb");
	if (jf.fp== NULL) {
		log_e("can not open file,%s\n",filename);
		return -1;
	}
	
	memset(jf.filename,0,sizeof(jf.filename));
	memcpy(jf.filename,filename,strlen(filename));


	ret =is_jpeg_file(filename,&jf);
	if(ret < 0 ){
		log_e("not jpeg file");
		
    	fclose(jf.fp);
		return -1;
	}

	ret = decode_jpeg(filename,&jf);
	if(ret < 0 ){
		log_e("decode jpeg wrong\n");
		fclose(jf.fp);
	}
	//***********************************************************************************************
#if 0
	log_i("jf.filename=%s\n",jf.filename);
	log_i("jf.width=%d\n",jf.width);
	log_i("jf.height=%d\n",jf.height);
	log_i("jf.Bpp=%d\n",jf.Bpp);
	log_i("jf.size=%d\n",jf.width*jf.height*jf.Bpp);
	log_i("jf.size=%d\n",jf.size);
#endif
	//***********************************************************************************************


	if(jf.width%4){
		v_w = jf.width + 4 - jf.width%4;
		swap_flag_w = 1;
	}
	else{
		v_w = jf.width;
	}
	
	if(jf.height%4){
		v_h = jf.height + 4 - jf.height%4;
		swap_flag_h = 1;
	}
	else{
		v_h = jf.height;
	}

	//***********************************************************************************************
#if 0
	log_i("swap_flag_h=%d\n",swap_flag_h);
	log_i("swap_flag_w=%d\n",swap_flag_w);	
	log_i("v_w=%d\n",v_w);
	log_i("v_h=%d\n",v_h);	
#endif
	//***********************************************************************************************

	if(swap_flag_w||swap_flag_h)
	{
		swap_buff = (char*)malloc(v_w*v_h*jf.Bpp);
		memset(swap_buff,0x00,v_w*v_h*jf.Bpp);

		for(i = 0;i < jf.height;i++)
		{
			memcpy(swap_buff + i*v_w*jf.Bpp,jf.buffer + i*jf.width*jf.Bpp ,jf.width*jf.Bpp);	
		}
	}
	else
	{
		swap_buff = jf.buffer;
	}
	
	//***********************************************************************************************
#if 0

	w = (LCD_WIDTH*jf.width/LCD_HEIGHT);

	h = (LCD_WIDTH*jf.height/LCD_HEIGHT);

	log_i("w  =%d\n",w );
	log_i("h  =%d\n",h );

	rga_control2(MODE_RESIZE,jf.buffer,jf.width,jf.height,jf.Bpp,
							out_buff,w,h,jf.Bpp);

#elif 1

	//log_i("src_w = %d\n",jf.width);
	//log_i("src_h = %d\n",jf.height);

	if((jf.width >= LCD_WIDTH)&&(jf.height >= LCD_HEIGHT))
	{
		//log_i("---1---\n");

		w = LCD_WIDTH;
		h = LCD_HEIGHT;

		out_buff = (char*)malloc(w*h*jf.Bpp);
		memset(out_buff,0x00,w*h*jf.Bpp);

		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,out_buff,w,h,jf.Bpp);
	}
	else if((jf.width >= LCD_WIDTH)&&(jf.height < LCD_HEIGHT))
	{
		//log_i("---2---\n");
	
		w = (jf.width*LCD_HEIGHT/jf.height);
		if(w%4){
			w = w + 4 - w%4;
		}
		h = LCD_HEIGHT;

		out_buff = (char*)malloc(w*h*jf.Bpp);
		memset(out_buff,0x00,w*h*jf.Bpp);
		
		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,out_buff,w,h,jf.Bpp);
	}
	else if((jf.width < LCD_WIDTH)&&(jf.height >= LCD_HEIGHT))
	{
		//log_i("---3---\n");

		w = (jf.width*LCD_HEIGHT)/jf.height;
		if(w%4){
			w = w + 4 - w%4;
		}
		h = LCD_HEIGHT;
		
		out_buff = (char*)malloc(w*h*jf.Bpp);
		memset(out_buff,0x00,w*h*jf.Bpp);

		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,out_buff,w,h,jf.Bpp);
	}
	else 
	{
		//log_i("---4---\n");
		
		w = jf.width;
		if(w%4){
			w = w + 4 - w%4;
		}
		h = jf.height;
		if(h%4){
			h = h + 4 - h%4;
		}	
		
		out_buff = (char*)malloc(w*h*jf.Bpp);
		memset(out_buff,0x00,w*h*jf.Bpp);

		rga_control2(MODE_RESIZE,swap_buff,v_w,v_h,jf.Bpp,out_buff,w,h,jf.Bpp);
	}
#endif
	//***********************************************************************************************

	show_jpeg_1(out_buff,w,h);

	//***********************************************************************************************					

	if(out_buff){
		free(out_buff);
		out_buff = NULL; 
	}
	
	if(swap_flag_w||swap_flag_h){
		if(swap_buff){
			free(swap_buff);
			swap_buff = NULL; 
		}
	}
	//***********************************************************************************************
	

	fclose(jf.fp);


	free_jpeg(&jf);
	
	//***********************************************************************************************
	
	return 0;

}





int lib_jpeg_init(void)
{
	log_i("lib jpeg init.\n");

	




	return 0;
}


int lib_jpeg_deinit(void)
{
	log_i("lib jpeg exit.\n");






	return 0;
}
